package com.ywgt.system.service.impl;

import com.ywgt.system.pojo.AuthUser;
import com.ywgt.system.pojo.User;
import com.ywgt.system.service.RoleService;
import com.ywgt.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.getUserByUsername(username);
        if (user==null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        List<String> roles = roleService.getRolesByUserName(username);
        AuthUser authUser=new AuthUser(username,user.getPassword(),roles,(user.getStatus()==1)?true:false);
        return authUser;
    }
}
